package com.example.cinema;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.Toast;

import com.example.cinema.databinding.ActivityMainBinding;
import com.example.cinema.drawview.BtmNavBar;
import com.example.cinema.toolclass.SvgaUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity {


    private boolean isbool = false;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(!isbool)
            {
                Toast.makeText(getApplicationContext(),"双击退出",Toast.LENGTH_SHORT).show();
                isbool = true;
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isbool = false;
                    }
                },1000);
            }else {
                Intent intent =new Intent();
                intent.setAction("android.intent.action.MAIN");
                intent.setAction("android.intent.category.HOME");
                startActivity(intent);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startSvga();
    }

    private ActivityMainBinding AMB;
    private UserAttribute attribute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        attribute = (UserAttribute) getApplication();
        AMB = DataBindingUtil.setContentView(this, R.layout.activity_main);
        ListenerInit();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }

    }

    //启动动画
    private void startSvga()
    {
        SvgaUtils svgaUtils = new SvgaUtils(getApplicationContext(),AMB.svgimg);
        svgaUtils.init();
        svgaUtils.startAnimator("remen");
    }



    private NavController Nc;
    private static int current = 0;
    private ValueAnimator vanimator;

    private void ListenerInit() {
        Nc = Navigation.findNavController(MainActivity.this, R.id.fragnav);
        AMB.btmnavbar.setOnItemClickListener(new BtmNavBar.OnItemClickListener() {
            @Override
            public void ItemClick(View view, int index) {
                switch (index) {
                    case 0:
                        if (current != 0) {
                            Nc.navigate(R.id.blankFragment1);
                            current = 0;
                            Log.i("EEEE", "生效中" + current);
                        }
                        break;
                    case 1:
                        if (current != 1) {
                            Nc.navigate(R.id.blankFragment0);
                            current = 1;
                        }
                        break;
                    case 2:
                        Toast.makeText(getApplicationContext(), "暂未开启", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent();
                        if(attribute.getToken()==null||attribute.getUID()==null)
                        {
                            intent.setAction("sign");
                        }else {
                            intent.setAction("livelist");
                        }
                        startActivity(intent);
                        break;

                    case 3:
                        if (current != 3) {
                            Nc.navigate(R.id.blankFragment2);
                            current = 3;
                        }
                        break;
                    case 4:
                        if (current != 4) {
                            Nc.navigate(R.id.blankFragment3);
                            current = 4;
                        }
                        break;
                }
            }
        });

        statrAnimator(AMB.xzimg);

    }

    //开启动画
    private void statrAnimator(View view) {
        vanimator = ValueAnimator.ofFloat(360);
        vanimator.setDuration(10000);
        vanimator.setInterpolator(new LinearInterpolator());
        vanimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float v = (float) animation.getAnimatedValue();
                AMB.xzimg.setRotation(v);
                if(v==360)
                {
                    statrAnimator(AMB.xzimg);
                }
            }
        });
        vanimator.start();
    }


}